/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;

import com.lunarshades.samsara.ui.SamsaraFrame;

/**
 * Action for saving data to a file store.  This implementation delegates
 * to SaveDataAction.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 * @see SaveDataAction
 */
public class SaveAsDataAction extends AbstractAction
        implements PropertyChangeListener {

public SaveAsDataAction(SaveDataAction saveAction) {
    super(NAME);
    mSaveAction = saveAction;

    URL smallIconUrl = getClass().getResource(
            "/com/lunarshades/samsara/ui/icons/saveAs16.gif"
    );
    putValue(Action.SMALL_ICON, new ImageIcon(smallIconUrl));
    putValue(Action.SHORT_DESCRIPTION, SHORT_TEXT);
    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
    setEnabled(false);
}

public void actionPerformed(ActionEvent e) {
    File store = mSaveAction.getFileStore();
    mSaveAction.setFileStore(null);
    if (!mSaveAction.execute()) {
        mSaveAction.setFileStore(store);
    }

}

public void propertyChange(PropertyChangeEvent evt) {
    String property = evt.getPropertyName();

    if (SamsaraFrame.DATA_MODIFIED.equals(property)) {
        setEnabled(true);
    } else if (SamsaraFrame.DATA_CLEAN.equals(property)) {
        setEnabled(false);
    } else if (LoadDataAction.FILE_LOADED.equals(property)) {
        setEnabled(true);
    } else if (RosterImportAction.DATA_IMPORTED.equals(property)) {
        setEnabled(true);
    }
}

private static final String NAME = "Save As ...";
private static final String SHORT_TEXT = "Save the current data set to file";
private final SaveDataAction mSaveAction;
}
